<extend name="Public/base"/>
<block name="header">
    <style>
        .imgbox{
            position:relative;
            float:left
        }
        .imgbox span{
            cursor:pointer;
            position:absolute;
            right:15px;
            top:5px;
            display:block;
            width:20px;
            height:20px;
            text-align:center;
            line-height:20px;
            background:rgba(0,0,0,0.5);
            color:#fff
        }

    </style>
</block>
<block name="body">
    <script type="text/javascript" src="__STATIC__/uploadify/jquery.uploadify.min.js"></script>
    <div class="main-title">
        <h2>{:isset($info['id'])?'编辑':'新增'}商品</h2>
    </div>
    <div class="tab-wrap">
        <ul class="tab-nav nav">
            <li data-tab="tab1" class="current"><a href="javascript:void(0);">通用信息</a></li>
            <li data-tab="tab2"><a href="javascript:void(0);">商品规格</a></li>
            <li data-tab="tab3"><a href="javascript:void(0);">商品相册</a></li>
        </ul>
        <div class="tab-content">
            <form action="{:U('Goods/commodity')}" method="post" class="form-horizontal">
                <!-- 基础 -->
                <div id="tab1" class="tab-pane in tab1">
                    <div class="form-item">
                        <label class="item-label">商品名称<span class="check-tips"></span></label>
                        <div class="controls">
                            <input type="text" class="text input-large" name="name" value="{$info.name|default=''}">
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">商品价格<span class="check-tips">(元)</span></label>
                        <div class="controls">
                            <input type="text" class="text input-small" name="price" value="{$info.price|default=''}"  onkeyup='this.value=this.value.replace(/[^\d.]/g,"")'>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">商品总库存<span class="check-tips">(个)</span></label>
                        <div class="controls">
                            <input type="text" class="text input-small" name="stock" value="{$info.stock|default=''}"  onkeyup='this.value=this.value.replace(/[^\d.]/g,"")'>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">商品所属分类<span class="check-tips"></span></label>
                        <div class="controls">
                            <select name="cat_id" id="change">
                                    <option value="0">请选择商品类型</option>
                                    <volist name="data" id="vo">
                                        <option value="{$vo.id}"<if condition="$info[cat_id] eq $vo[id]"> selected="selected" </if>>{$vo.title}</option>
                                    </volist>
                            </select>
                            <select name="extend_cat_id"style="width:100px; ">
                                <option id="quern">请选择</option>
                                <if condition="$title">
                                    <option selected>{$title}</option>
                                </if>
                            </select>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">是否新品<span class="check-tips"></span></label>
                        <div class="controls">
                            <label class="radio"><input type="radio" name="is_new" value="1" <if condition="$info[is_new] eq 1"> selected="selected" </if>>是</label>
                            <label class="radio"><input type="radio" name="is_new" value="0" <if condition="$info[is_new] eq 0"> selected="selected" </if>checked>否</label>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">是否热销<span class="check-tips"></span></label>
                        <div class="controls">
                            <label class="radio"><input type="radio" name="is_hot" value="1"<if condition="$info[is_hot] eq 1"> selected="selected" </if>>是</label>
                            <label class="radio"><input type="radio" name="is_hot" value="0" <if condition="$info[is_hot] eq 0"> selected="selected" </if>checked>否</label>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">是否置顶<span class="check-tips"></span></label>
                        <div class="controls">
                            <label class="radio"><input type="radio" name="is_top" value="1" <if condition="$info[is_top] eq 1"> selected="selected" </if>>是</label>
                            <label class="radio"><input type="radio" name="is_top" value="0" <if condition="$info[is_top] eq 0"> selected="selected" </if>checked>否</label>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">商品简介</label>
                        <div class="controls">
                            <label class="textarea input-large">
                                <textarea name="brief">{$info.brief}</textarea>
                            </label>
                        </div>
                    </div>
                    <div class="form-item">
                        <label class="item-label">图文详情<span class="check-tips"></span></label>
                        <div class="controls">
                            <textarea name="details">{$info['details']}</textarea>
                            {:hook('adminArticleEdit', array('name'=>details,'value'=>$info['details']))}
                        </div>
                    </div>
                </div>
                <!-- 商品规格 -->
                <div id="tab2" class="tab-pane tab2">
                    <div class="form-item">
                        <label class="item-label">商品类型<span class="check-tips"></span></label>
                        <div class="controls">
                            <select name="spec_type" id="spec_type">
                                <option value="0">请选择商品类型</option>
                                <volist name="goods" id="vo">
                                    <option value="{$vo.id}" <if condition="$info[spec_type] eq $vo[id]"> selected="selected" </if>>{$vo.name}</option>
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div id="ajax_spec_data"><!-- ajax 返回规格--></div>
                </div>
                <!-- 商品相册 -->
                <div id="tab3" class="tab-pane tab3">
                    <div class="form-item">
                        <label class="item-label">图片上传<span class="check-tips"></span></label>
                        <div class="controls" id="up_header_img">
                            <div class="upload-box"><img src="" id="imgView" style="height:100px; display:none"><input id="upfeng" type="file"/></div><div id="upload-img-tip"></div>
                        </div>
                        <if condition="$datas">
                            <foreach name="datas.explode_photo_list.path" item="vo" key="ko">
                                <dd class="btn-file">
                                    <img src="{$vo}" class="showImg deleImg" data-id="{$ko}" style="width: 100px; height: 100px;">
                                </dd>
                            </foreach>
                        </if>
                    </div>
                    <div class="form-item">
                        <input type="hidden" name="goods_id" value="{$info.id|default=''}">
                        <button type="submit" id="submit" class="btn submit-btn ajax-post" target-form="form-horizontal">确 定</button>
                        <button class="btn btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
                    </div>
                </div>


            </form>
        </div>
    </div>
</block>
<block name="script">
    <script type="text/javascript">
        $(function(){
            showTab();
            $("input[name=reply]").change(function(){
                var $reply = $(".form-item.reply");
                parseInt(this.value) ? $reply.show() : $reply.hide();
            }).filter(":checked").change();
        });
        //导航高亮
        highlight_subnav('{:U('index')}');
    </script>
    <script type="text/javascript">
        $('#upfeng').uploadify({
            'auto' : true,
            'removeTimeout' : 1,//文件队列上传完成1秒后删除
            'swf' : "__STATIC__/uploadify/uploadify.swf",
            'uploader' :"{:U('Banner/uploadPath')}",
//            'method' : 'post', //方法，服务端可以用$_POST数组获取数据
            'buttonText' : '上传图片 ',//设置按钮文本
            "width": 120,		//设置按钮宽
            "height": 100,	//设置按钮高
            'multi' : true,	//允许同时上传多张图片
            'fileTypeDesc' : 'Image Files',//只允许上传图像
            'fileTypeExts' : '*.gif;*.jpg;*.png;*.bmp;*.jpeg',//限制允许上传的图片后缀
            'fileSizeLimit': '5242880',
            'queueID': 'upload-img-tip',
            'onUploadSuccess' :function(file, data,response) {
                var data = $.parseJSON(data);
                var length = $('.upload-box img').length;
                if(data.status==0){
                    alert(data.info);
                    return false
                }
                if(length>3){
                    alert('最多允许上传3张图片！');
                    return false
                }
                $(".upload-box").prepend(
                        '<div class="imgbox"><span>X</span>'+
                        '<img src="'+data.path+'" class="imgView" style="height:110px;">'+
                        '<input type="hidden" name="img[]" value="'+data.src+'"/></div>'
                );
            },
            'onFallback' : function() {
                alert('未检测到兼容版本的Flash.');
            }
        });
    </script>
    <!--商品分类 start-->
    <script type="text/javascript">
        $("#change").change(function(){
            var title=$(this).children('option:selected').val();
                   if(title==0){
                       alert('请选择分类');
                       return false;
                  }
            $.ajax({
                type:"POST",
                url:"{:U('Goods/cate')}",
                data:{title:title},
                dataType:"json",
                success:function(data){
                      var str='';
                    $.each(data, function(){
                        str+='<option value="'+this.id+'">'+this.title+'</option>'
                    });
                    $("#quern").nextAll().remove();
                    $("#quern").after(str);
                }
            })
        })
    </script>
    <!--商品分类 end-->
    <!--商品规格 start-->
    <script type="text/javascript">
        /** 以下 商品规格相关 js*/
        $(document).ready(function(){
            // 商品类型切换时 ajax 调用  返回不同的属性输入框
            $("#spec_type").change(function(){
                var goods_id = $("input[name='goods_id']").val();
                var spec_type = $(this).val();
                $.ajax({
                    type:'GET',
                    data:{goods_id:goods_id,spec_type:spec_type},
                    url:"{:U('Goods/ajaxGetSpecSelect')}",
                    success:function(data){
                        $("#ajax_spec_data").html('')
                        $("#ajax_spec_data").append(data);
                        //alert('132');

                        ajaxGetSpecInput();	// 触发完  马上处罚 规格输入框
                    }
                });
            });
            // 触发商品规格
            $("#spec_type").trigger('change');
        });

        //删除图片
        $(document).on('click','.imgbox span',function(){
            var $box = $(this).parent('.imgbox');
            var img=$(this).nextAll("input[name='img[]']").val();
            $.ajax({
                type:"POST",
                url:"{:U('Banner/delPath')}",
                data:{id:img},
                success:function(data){
                    if(data.status==1){
                        $box.fadeOut('500',function(){
                            $(this).remove()
                        });
                    }
                }
            })
        });
    </script>
</block>
